/**
 * 将对象转成请求参数
 */
export const objectToQueryString = (params: Object) => {
    let result = "?"
    for (let key in params) {
        const value = params[key]
        result += `${key}=${value}&`
    }
    return result
}

/**
 * 字符串转hash
 */
export const stringToHash = (str: string): string => {
    let hash = 0, i, chr;
    if (str.length === 0) return hash.toString();
    for (i = 0; i < str.length; i++) {
        chr   = str.charCodeAt(i);
        hash  = ((hash << 5) - hash) + chr;
        hash |= 0;
    }
    return hash.toString();
}

/**
 * 获取当前时间 格式 yyyy-mm-dd hh:ii:ss
 */
export const getNowTime =  () => {
    const d = new Date()
    const year = d.getUTCFullYear();
    const month = d.getMonth() + 1;
    const date = d.getDate();
    const hours = d.getHours();
    const minutes = d.getMinutes();
    const seconds = d.getSeconds();
    let curDateTime= `${year}`;
    curDateTime = month > 9 ? `${curDateTime}-${month}` : `${curDateTime}-0${month}`
    curDateTime = date > 9 ? `${curDateTime}-${date}` : `${curDateTime}-0${date}`
    curDateTime = hours > 9 ? `${curDateTime} ${hours}` : `${curDateTime} 0${hours}`
    curDateTime = minutes > 9 ? `${curDateTime}:${minutes}` : `${curDateTime}:0${minutes}`
    curDateTime = seconds > 9 ? `${curDateTime}:${seconds}` : `${curDateTime}:0${seconds}`
    return curDateTime;
}

/**
 * 根据字体大小，窗口宽度切割字符串
 */
export const stringToLines = (str: string, fontSize: number, width: number) => {
    const size = Math.floor(width / fontSize *4 )
    console.log("width"+width)
    console.log("fontSize"+fontSize)
    console.log("size"+size)
    let result = []
    str.split("\n").forEach(line => {
        let lines = getLines(line, size)
        result = result.concat(lines)
    })
    return result
}

const getLines = (str, size) => {
    const result = [];
    let temp = ""
    let tempLen = 0
    for (let i = 0; i < str.length; i++) {
        let len = getStringLen(str[i])
        if (tempLen + len <= size) {
            tempLen = tempLen + len
            temp = temp + str[i]
        }
        else {
            if (len == 4 && !str[i].match(/[\u4e00-\u9fa5]/g)) {
                let j = 1
                while(true) {
                    if (str[i-j].match(/[\u4e00-\u9fa5]/g)) {
                        break
                    }
                    j++
                }

                temp = temp.substr(0, temp.length-j);
                result.push(temp)
                tempLen = 0
                temp = ""
                for(let jj = j;jj>=0;jj--) {
                    temp += str[i-jj]
                    tempLen += getStringLen(str[i-jj])
                }
            }
            else {
                if (str.charCodeAt(i) === 32 && str.charCodeAt(i-1) === 32) {
                    temp = temp + str[i]
                }
                else {
                    result.push(temp)
                    tempLen = getStringLen(str[i])
                    temp = str[i]
                }
            }
        }
    }
    if (temp.length > 0) result.push(temp)
    return result
}

const getStringLen = (str) => {
    const reg = /[\u3002|\uff1f|\uff01|\uff0c|\u3001|\uff1b|\uff1a|\u201c|\u201d|\u2018|\u2019|\uff08|\uff09|\u300a|\u300b|\u3008|\u3009|\u3010|\u3011|\u300e|\u300f|\u300c|\u300d|\ufe43|\ufe44|\u3014|\u3015|\u2026|\u2014|\uff5e|\ufe4f|\uffe5]/
    if (str.match(/[\u4e00-\u9fa5]/g)) {
        return 4
    }
    if (str.match(/[\uff00-\uffff]/g)) {
        return 4
    }
    if (reg.test(str))
        return 4
    if (str.charCodeAt(0) === 12288) return 4
    return 2
}
